12 June 2019

Diamonds dataset

We are going to build a plot using plotly for Price vs Carat and then compare how it varies with diamond cuts.

suppressPackageStartupMessages(library(ggplot2))
set.seed(1345)
data("diamonds")
d <- diamonds[sample(nrow(diamonds), 500), ]
head(d)
## # A tibble: 6 x 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 1.03  Very Good G     VS1      60.7    58  6628  6.5   6.55  3.96
## 2 0.32  Ideal     D     VS1      60.2    57   933  4.45  4.42  2.67
## 3 0.290 Very Good H     VVS2     61.7    55   541  4.27  4.3   2.64
## 4 0.9   Good      I     VS2      58.5    66  3353  6.23  6.25  3.65
## 5 1.01  Very Good H     SI2      60.6    61  3888  6.38  6.45  3.89
## 6 0.4   Ideal     G     VVS2     61.4    53   883  4.73  4.81  2.93

Slide with Plot

suppressPackageStartupMessages(library(plotly))
p <- plot_ly(d, x=~carat, y=~price, mode="markers", color=as.factor(d$cut))
add_trace(p, type = "scatter")